package com.pwc.conversionTool.controller;

import static com.pwc.GAE.blobstore.BlobstoreUtil.createUploadUrl;
import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.blobstore.BlobInfo;
import com.pwc.GAE.blobstore.BlobstoreManager;
import com.pwc.conversionTool.biz.ProjectBiz;
import com.pwc.conversionTool.biz.TaskBiz;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.Log;
import com.pwc.conversionTool.entity.Project;
import com.pwc.conversionTool.entity.TaskFlow;

@Controller
public class TaskController {

	private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
	@Autowired
	public ServletContext context;

	private BlobstoreManager manager;

	public ProjectBiz projectBiz = new ProjectBiz();

	public TaskBiz taskBiz = new TaskBiz();

	@RequestMapping(value = "/task/taskFlow", method = RequestMethod.GET)
	public String createTaskFlow(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		return "task/taskFlow";
	}

	@RequestMapping(value = "/task/manageTask", method = RequestMethod.GET)
	public String manageTask(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		return "task/manageTaskFlow";
	}

	@RequestMapping(value = "/task/addTask", method = RequestMethod.POST)
	public String addTask(TaskFlow taskFlow, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		List<Project> projects = null;

		TaskFlow result = null;
		try {
			
			if(taskFlow.getId()!=null)
			{
				result= taskBiz.getTaskFlowById(taskFlow.getId());
			}else {
				result = taskBiz.createTaskFlow(taskFlow.getTaskFlowName(), getCurrentAccount(session).getKey());
			}
			
			projects = projectBiz.getProjectByAccount(getCurrentAccount(session));
			model.addAttribute("taskFlow", result);
			model.addAttribute("projects", projects);
			model.addAttribute("csvFileUpload", createUploadUrl("/task/csvTypeUpload"));
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return "task/task";
	}

	@RequestMapping(value = "/task/csvTypeUpload", method = RequestMethod.POST)
	public String csvTypeUpload(
			@RequestParam("taskFlowId") Long taskFlowId, 
			@RequestParam("id") Long id, 
			@RequestParam("taskName") String taskName, 
			@RequestParam("operationId") Long operationId,
			@RequestParam("errorPath") String errorPath,
			@RequestParam("errorValue") String errorValue,
			 HttpServletRequest req, Locale locale, Model model, HttpSession session) {		
	
		try {
			manager = new BlobstoreManager(req);
			List<BlobInfo>  infos = manager.getMutiBlobInfos("file");
			
			taskBiz.saveTask(taskFlowId,id,taskName,operationId,errorPath,errorValue,infos);
			
			//get task flow
			
			model.addAttribute("taskFlow", taskBiz.getTaskFlowById(taskFlowId));
			
		} catch (Exception e) {

			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return "task/taskFlow";
	}
	
	@RequestMapping(value = "/task/viewLogDetail", method = RequestMethod.GET)
	public String viewLogDetail(String type,Long id,Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		Log log=taskBiz.getLogInfo(type, id);
		
		model.addAttribute("log", log);

		return "task/logDetail";
	}
	
	
	@RequestMapping(value = "/task/viewTaskFlow", method = RequestMethod.GET)
	public String viewTaskFlow(Long id,Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		model.addAttribute("taskFlow", taskBiz.getTaskFlowById(id));

		return "task/taskFlow";
	}
	
	@RequestMapping(value = "/task/viewTaskData", method = RequestMethod.GET)
	public String viewTaskData(Long id,Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		model.addAttribute("taskId", id);

		return "task/viewData";
	}
	
	
	

}
